package com.hdoit.ares.web.weixin;

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

import com.tennetcn.common.selfcommon.message.wechat.Button;
import com.tennetcn.common.selfcommon.message.wechat.ComplexButton;
import com.tennetcn.common.selfcommon.message.wechat.Menu;
import com.tennetcn.common.selfcommon.message.wechat.ViewButton;
import com.tennetcn.common.utils.JsonUtils;

public class WxMenuCreateTest02 {
	//@Test
	public void createMenu(){
		WxMpService wxService = new WxMpServiceImpl(); 
		wxService.setWxMpConfigStorage(getConfig());
		
		String appId=wxService.getWxMpConfigStorage().getAppId();
		String menuJson=getMenu(appId);	
		
		System.out.println(menuJson);
		
		try {
			String menuId=wxService.getMenuService().menuCreate(menuJson);
			System.out.println(menuId);
		} catch (Exception e) {
			
		}
	}
	
	private String getMenu(String appId){
		//String domain="http://www.putiyoli.net.cn";
		String domain="http://www.hdoit.com";
		
		ViewButton exclusiveService = new ViewButton();
		exclusiveService.setName("专属服务");
		exclusiveService.setType("view");
		exclusiveService.setUrl(domain+"/ares/wechat/home/index");
		
		
		ViewButton myCodeButton = new ViewButton();
		myCodeButton.setName("我的邀请码");
		String discountViewUrl=domain+"/ares/wechat/cust/personCenter/shareCode";
		myCodeButton.setType("view");
		myCodeButton.setUrl(discountViewUrl);
		
		
		
		ComplexButton imSharedButton = new ComplexButton();
		imSharedButton.setName("我要分享");
		imSharedButton.setSub_button(new Button[] {myCodeButton});
		
		
		ViewButton personCenter = new ViewButton();
		personCenter.setName("个人中心");
		String personCenterViewUrl=domain+"/ares/wechat/cust/personCenter/index";
		personCenter.setType("view");
		personCenter.setUrl(personCenterViewUrl);

		
		ComplexButton meButton = new ComplexButton();
		meButton.setName("我的");
		meButton.setSub_button(new Button[] {personCenter});
		
		
		Menu menu = new Menu();
		menu.setButton(new Button[] { exclusiveService,imSharedButton, meButton });
		
		
		return JsonUtils.toJson(menu);
	}
	
	private WxMpInMemoryConfigStorage getConfig(){
		WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
		config.setAppId("wx8a0b70c531c68f29"); // 设置微信公众号的appid
		config.setSecret("294d5f4974ac6aea3d2b16bbac57f855"); // 设置微信公众号的app corpSecret
//		config.setAppId("wx62ca56fd226591a6"); // 设置微信公众号的appid
//		config.setSecret("17f88d85868b7a34ea141e7eb36e7812"); 
		config.setToken(""); // 设置微信公众号的token
		config.setAesKey(""); // 设置微信公众号的EncodingAESKey
		
		return config;
	}
}